package com.mentaleye.biz.impl;

import com.mentaleye.bean.ProcessDefinitionBean;
import com.mentaleye.biz.DeployBiz;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.ObjectUtil;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

@Service
public class DeployBizImpl
  implements DeployBiz
{
  private static Logger logger = Logger.getLogger(DeployBizImpl.class);

  @Autowired
  protected RepositoryService repositoryService;

  public void getProcessDefinitionListPage(CommonPage commonPage) { List list = null;
    String deploymentId = commonPage.getParam("deploymentId");
    String nameLike = commonPage.getParam("nameLike");
    String keyLike = commonPage.getParam("keyLike");

    String active = commonPage.getParam("active");
    try {
      ProcessDefinitionQuery query = this.repositoryService.createProcessDefinitionQuery();

      processDefinitionQuery(query, deploymentId, nameLike, keyLike, false, active);
      list = ObjectUtil.transferList(query.listPage(commonPage.getiDisplayStart().intValue(), commonPage.getiDisplayLength().intValue()), ProcessDefinitionBean.class);
      commonPage.setiTotalRecords(Integer.valueOf((int)query.count()));
    } catch (Exception e) {
      logger.error("查询流程定义列表异常", e);
    }
    commonPage.setPageData(list);
  }

  private void processDefinitionQuery(ProcessDefinitionQuery query, String deploymentId, String nameLike, String keyLike, boolean islast, String active)
  {
    if (StringUtils.isNotEmpty(deploymentId)) {
      query.deploymentId(deploymentId);
    }

    if (StringUtils.isNotEmpty(nameLike)) {
      query.processDefinitionNameLike(nameLike);
    }

    if (StringUtils.isNotEmpty(keyLike)) {
      query.processDefinitionKeyLike(keyLike);
    }
    if (islast) {
      query.latestVersion();
    }
    if (StringUtils.isNotEmpty(active))
      if ("true".equals(active))
        query.active();
      else
        query.suspended();
  }

  public List<ProcessDefinitionBean> getProcessDefinitionList(String deploymentId, String nameLike, String keyLike, boolean islast, String active)
  {
    try
    {
      ProcessDefinitionQuery query = this.repositoryService.createProcessDefinitionQuery();
      processDefinitionQuery(query, deploymentId, nameLike, keyLike, islast, active);

      return ObjectUtil.transferList(query.list(), ProcessDefinitionBean.class);
    }
    catch (Exception e) {
      logger.error("查询流程定义列表异常", e);
    }return null;
  }

  public boolean deployFile(MultipartFile fis)
  {
    try
    {
      ZipInputStream zi = new ZipInputStream(fis.getInputStream());
      this.repositoryService.createDeployment().addZipInputStream(zi).deploy();
    } catch (Exception e) {
      logger.error("流程部署异常", e);
      return false;
    }
    return true;
  }

  public InputStream resourceAsStream(String processDefinitionId, String resourceType)
  {
    try {
      ProcessDefinition processDefinition = (ProcessDefinition)this.repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).singleResult();
      String resourceName = "";
      if (resourceType.equals("image"))
        resourceName = processDefinition.getDiagramResourceName();
      else if (resourceType.equals("xml")) {
        resourceName = processDefinition.getResourceName();
      }
      return this.repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), resourceName);
    } catch (Exception e) {
      logger.error("流程定义资源异常", e);
    }return null;
  }

  public boolean deleteDeploy(String deploymentId)
  {
    boolean success = false;
    try {
      this.repositoryService.deleteDeployment(deploymentId);
      success = true;
    } catch (Exception e) {
      logger.error("删除流程部署异常", e);
    }
    return success;
  }

  public boolean activateOrSuspendProcessDefinition(String processDefinitionId, boolean active)
  {
    boolean success = false;
    try
    {
      if (active)
        this.repositoryService.activateProcessDefinitionById(processDefinitionId);
      else {
        this.repositoryService.suspendProcessDefinitionById(processDefinitionId);
      }
      success = true;
    } catch (Exception e) {
      if (active)
        logger.error("流程激活异常processDefinitionId:" + processDefinitionId, e);
      else {
        logger.error("流程挂起异常processDefinitionId:" + processDefinitionId, e);
      }
    }

    return success;
  }
}